1 /* binary search tree */
6 #define compLT(a,b) (a < b)
7 #define compEQ(a,b) (a == b)
9 /* implementation dependent declarations */
17 typedef int keyType
; /* type of key */
19 /* user data stored in tree */
21 int stuff
; /* optional related data */
24 typedef struct nodeTag
{
25 struct nodeTag
*left
; /* left child */
26 struct nodeTag
*right
; /* right child */
27 struct nodeTag
*parent
; /* parent */
28 keyType key
; /* key used for searching */
29 recType rec
; /* user data */
32 nodeType
*root
= NULL
; /* root of binary tree */
34 statusEnum
insert(keyType key
, recType
*rec
) {
35 nodeType
*x
, *current
, *parent
;
37 /***********************************************
38 * allocate node for data and insert in tree *
39 ***********************************************/
41 /* find future parent */
45 if (compEQ(key
, current
->key
))
46 return STATUS_DUPLICATE_KEY
;
48 current
= compLT(key
, current
->key
) ?
49 current
->left
: current
->right
;
53 if ((x
= malloc (sizeof(*x
))) == 0) {
54 return STATUS_MEM_EXHAUSTED
;
62 /* insert x in tree */
64 if(compLT(x
->key
, parent
->key
))
74 statusEnum
delete(keyType key
) {
77 /***************************
78 * delete node from tree *
79 ***************************/
81 /* find node in tree */
84 if(compEQ(key
, z
->key
))
87 z
= compLT(key
, z
->key
) ? z
->left
: z
->right
;
89 if (!z
) return STATUS_KEY_NOT_FOUND
;
91 /* find tree successor */
92 if (z
->left
== NULL
|| z
->right
== NULL
)
96 while (y
->left
!= NULL
) y
= y
->left
;
99 /* point x to a valid child of y, if it has one */
105 /* remove y from the parent chain */
106 if (x
) x
->parent
= y
->parent
;
108 if (y
== y
->parent
->left
)
111 y
->parent
->right
= x
;
115 /* if z and y are not the same, copy y to z. */
125 statusEnum
find(keyType key
, recType
*rec
) {
127 /*******************************
128 * find node containing data *
129 *******************************/
131 nodeType
*current
= root
;
132 while(current
!= NULL
) {
133 if(compEQ(key
, current
->key
)) {
137 current
= compLT(key
, current
->key
) ?
138 current
->left
: current
->right
;
141 return STATUS_KEY_NOT_FOUND
;
144 int main(int argc
, char **argv
) {
145 int i
, maxnum
, random
;
154 * bin 5000 // 5000 sequential
155 * bin 2000 r // 2000 random
158 maxnum
= atoi(argv
[1]);
161 if ((rec
= malloc(maxnum
* sizeof(recType
))) == 0) {
162 fprintf (stderr
, "insufficient memory (rec)\n");
165 if ((key
= malloc(maxnum
* sizeof(keyType
))) == 0) {
166 fprintf (stderr
, "insufficient memory (key)\n");
170 if (random
) { /* random */
171 /* fill "key" with unique random numbers */
172 for (i
= 0; i
< maxnum
; i
++) key
[i
] = rand();
173 printf ("ran bt, %d items\n", maxnum
);
175 for (i
=0; i
<maxnum
; i
++) key
[i
] = i
;
176 printf ("seq bt, %d items\n", maxnum
);
180 for (i
= 0; i
< maxnum
; i
++) {
181 status
= insert(key
[i
], &rec
[i
]);
182 if (status
) printf("pt1, i=%d: %d\n", i
, status
);
185 for (i
= maxnum
-1; i
>= 0; i
--) {
186 status
= find(key
[i
], &rec
[i
]);
187 if (status
) printf("pt2, i=%d: %d\n", i
, status
);
190 for (i
= maxnum
-1; i
>= 0; i
--) {
191 status
= delete(key
[i
]);
192 if (status
) printf("pt3, i=%d: %d\n", i
, status
);